Skip to content

Send Email Notification on Chatbot Answer Change#421

Open
ribhavsharma wants to merge 12 commits intomainfrom
ribhav/chatbot-answer-changed-notif
Open

Send Email Notification on Chatbot Answer Change#421
ribhavsharma wants to merge 12 commits intomainfrom
ribhav/chatbot-answer-changed-notif

Conversation

@ribhavsharma
Copy link
Collaborator

Description

Added a checkbox in the edit chatbot answer modal, that lets the professor send out notifications to relevant students when a chatbot answer is changed.

Closes #305

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • This requires a run of yarn install
  • This change requires an addition/change to the production .env variables. These changes are below:
  • This change requires developers to add new .env variables. The file and variables needed are below:
  • This change requires a database query to update old data on production. This query is below:

How Has This Been Tested?

Tested locally, the correct email shows up in the "sent emails" log file.

Checklist:

  • I have performed a code review of my own code (under the "Files Changed" tab on github) to ensure nothing is committed that shouldn't be (e.g. leftover console.logs, leftover unused logic, or anything else that was accidentally committed)
  • I have commented my code where needed
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that new and existing tests pass locally with my changes
  • Any work that this PR is dependent on has been merged into the main branch
  • Any UI changes have been checked to work on desktop, tablet, and mobile

Copy link
Collaborator

@AdamFipke AdamFipke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

<br/>
<p>You can revisit the course chatbot from your course page.</p>
</div>
`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the body of this endpoint should be moved into its own method inside chatbot.service.ts since controllers are technically supposed to just handle like error checking and then calling the right services, and thus are supposed to be pretty slim.

ChatbotService,
ChatbotApiService,
ChatbotSettingsSubscriber,
MailService,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh interesting, i guess you didn't need to add MailModule to the imports array for some reason?

TestTypeOrmModule,
TestConfigModule,
FactoryModule,
ChatbotModule.forRoot(TestChatbotConnectionOptions),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait... what?

I didn't really touch the lms stuff but i feel like removing the chatbotmodule would break tests but idk. @bhunt02

@AdamFipke
Copy link
Collaborator

Oh right, and maybe adding a handful of integration or service tests for this would be a pretty good idea!

@AdamFipke AdamFipke mentioned this pull request Nov 26, 2025
16 tasks
Copy link
Collaborator

@AdamFipke AdamFipke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the feedback. I also manually tested it and it seems to work! Going over it I commited a couple small things.

Main things are some comments about tests. The feedback there could also be applied to your other PRs.

I would still see about moving most of the method body of notifyUpdatedAnswer() endpoint into its own function inside chatbot.service.ts for organization reasons.

Comment on lines +171 to +173
const { emailNotifyOnAnswerUpdate, ...sanitizedValues } = values
const valuesWithId = {
...values,
...sanitizedValues,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh neat I didn't know you could use the spread operator when deconstructing an object

});
});

describe('POST /chatbot/question/:courseId/:vectorStoreId/notify', () => {
Copy link
Collaborator

@AdamFipke AdamFipke Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would possibly add a few more tests for:

  • Making sure only professors and TAs can use it
  • Making sure it only notifies students who asked the question, not other students in the course.
  • Make sure it still succeeds for empty strings for old and new answer
  • Tests to make sure the DTO is working:
    • Making sure it fails if oldAnswer is undefined
    • Making sure it fails if newAnswer is undefined
    • Make sure you can't send integers or other data types for oldAnswer, newAnswer, oldQuestion, and newQuestion

You may also find it nicer to refactor a bunch of your tests into helper functions (you can see an example in @bhunt02 's PR here https://github.com/ubco-db/helpme/pull/351/files#diff-8d3cece12bb8c1d5223de78a3e45bee5b3becbb4dfef50609332fac922bebd6cR737.

You can probably just get away with using AI to generate these (just use a smart model like gemeni 3 pro or opus/sonnet 4.5) and make sure to go over it so there's no glaring logic issues.

It would also be nice if there was a test to make sure the email didn't have "null" or stuff like that, but that might be a little more effort than it's worth (since it might involve modifying the email test util functions and saving a snapshot). Maybe look into it?

import { BadRequestException } from '@nestjs/common';
import { NotifyUpdatedChatbotAnswerParams, Role } from '@koh/common';

describe('ChatbotController - notifyUpdatedAnswer', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is kinda weird. This is like an integration test but with a few more details abstracted away, and it doesn't really follow the rest of the project.

Having just the chatbot.integration.ts tests are enough (since that tests calling the whole endpoint). Once you move most of the body of the notifyUpdatedAnswer to its own function inside chatbot.service.ts, you could make some corresponding unit tests inside chatbot.service.spec.ts, but it's usually not necessary since the integration tests already cover it. Though, having unit tests may be nicer if you want to test for very specific behaviour by mocking a bunch of things (e.g. for making sure the email body is correct).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think chatbot.service.ts does a lot of jobs already... should the FX for notifying not be delegated to a corresponding proviser?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think chatbot.service.ts does a lot of jobs already... should the FX for notifying not be delegated to a corresponding proviser?

Hmm maybe. Are you thinking a small-scoped provider (e.g. chatbot-email-notification.service.ts) or larger provider (e.g. notification-email.service.ts)?

Though I don't see us adding many more email notifications to do with the chatbot. And a larger one wouldn't make much sense imo since then it's a big random service file that gets imported to a bunch of other files for only 1 function.

If we're concerned about the size of chatbot.service.ts, it woudl probably make more sense to refactor out the ChatbotOrganizationSettings (or just ChatbotSettings) stuff into its own service.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No what I mean is single responsibility principle. Notifications should be handled by one provider, not many

All the chatbot stuff falls under the purview of the chatbot provider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option to Email Notify students who asked this chatbot question when modifying answer

3 participants